package org.behrang.macbeans.tab.bm;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.plaf.ComponentUI;
import org.netbeans.swing.tabcontrol.SlideBarDataModel;
import org.netbeans.swing.tabcontrol.SlidingButton;
import org.netbeans.swing.tabcontrol.SlidingButtonUI;

/**
 *
 * @author behrangsa
 */
public class BMSlidingButtonUI extends SlidingButtonUI {

    private static JButton renderer = new JButton();
    

    static {
        renderer.putClientProperty("JButton.buttonType", "recessed");        
    }

    public static ComponentUI createUI(JComponent c) {
        return new BMSlidingButtonUI();
    }

    public BMSlidingButtonUI() {        
    }

    @Override
    protected void paintBackground(Graphics2D g, AbstractButton b) {
        SlidingButton sb = (SlidingButton) b;
        if (sb.getOrientation() == SlideBarDataModel.SOUTH) {
            renderer.setText(b.getText());
            renderer.setIcon(sb.getIcon());
            Dimension dim = renderer.getPreferredSize();
            // dim.width += 20;
            renderer.setSize(dim);
            renderer.setLocation(0, 0);
            g.translate(-4, 0);
            renderer.paint(g);
            renderer.setText("");
            g.translate(4, 0);
        } else if (sb.getOrientation() == SlideBarDataModel.EAST) {
            renderer.setText(b.getText());
            renderer.setIcon(sb.getIcon());
            renderer.setSelected(false);
            Dimension dim = renderer.getPreferredSize();
            // dim.width += 20;
            renderer.setSize(dim);
            renderer.setLocation(0, 0);
            BufferedImage bi = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_4BYTE_ABGR);
            renderer.paint(bi.getGraphics());
            renderer.setText("");
            
            Graphics2D g2 = (Graphics2D) g;
            g2.translate(dim.height, -4);
            g2.rotate(90.0 * Math.PI / 180.0);
            g.drawImage(bi, 0, 0, null);
            g2.rotate(-90.0 * Math.PI / 180.0);
            g2.translate(-dim.height, 4);
        } else {
            renderer.setText(b.getText());
            renderer.setIcon(sb.getIcon());
            renderer.setSelected(false);
            Dimension dim = renderer.getPreferredSize();
            // dim.width += 20;
            renderer.setSize(dim);
            renderer.setLocation(0, 0);
            BufferedImage bi = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_4BYTE_ABGR);
            renderer.paint(bi.getGraphics());
            renderer.setText("");
            
            Graphics2D g2 = (Graphics2D) g;
            g2.translate(0, dim.width);
            g2.rotate(-90.0 * Math.PI / 180.0);
            g.drawImage(bi, 0, 0, null);
            g2.rotate(90.0 * Math.PI / 180.0);
            g2.translate(0, -dim.width);
        }
    }

    @Override
    protected void paintButtonPressed(Graphics g, AbstractButton b) {
        SlidingButton sb = (SlidingButton) b;                
        if (sb.getOrientation() == SlideBarDataModel.SOUTH) {
            renderer.setText(b.getText());
            renderer.setIcon(sb.getIcon());
            renderer.setSelected(true);
            Dimension dim = renderer.getPreferredSize();
            // dim.width += 20;
            renderer.setLocation(0, 0);
            renderer.setSize(dim);
            g.translate(-4, 0);
            renderer.paint(g);
            renderer.setText("");
            g.translate(4, 0);
            renderer.setSelected(false);
        } else if (sb.getOrientation() == SlideBarDataModel.EAST) {
            renderer.setText(b.getText());
            renderer.setIcon(sb.getIcon());
            renderer.setSelected(true);
            Dimension dim = renderer.getPreferredSize();
            // dim.width += 20;
            renderer.setSize(dim);
            renderer.setLocation(0, 0);
            BufferedImage bi = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_4BYTE_ABGR);
            renderer.paint(bi.getGraphics());
            renderer.setSelected(false);
            renderer.setText("");            
            
            Graphics2D g2 = (Graphics2D) g;
            g2.translate(dim.height, -4);
            g2.rotate(90.0 * Math.PI / 180.0);
            g.drawImage(bi, 0, 0, null);
            g2.rotate(-90.0 * Math.PI / 180.0);
            g2.translate(-dim.height, 4);
        } else {
            renderer.setText(b.getText());
            renderer.setIcon(sb.getIcon());
            renderer.setSelected(true);
            Dimension dim = renderer.getPreferredSize();
            // dim.width += 20;
            renderer.setSize(dim);
            renderer.setLocation(0, 0);
            BufferedImage bi = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_4BYTE_ABGR);            
            renderer.paint(bi.getGraphics());
            renderer.setSelected(false);
            
            renderer.setText("");
            Graphics2D g2 = (Graphics2D) g;
            g2.translate(0, dim.width);
            g2.rotate(-90.0 * Math.PI / 180.0);
            g.drawImage(bi, 0, 0, null);
            g2.rotate(90.0 * Math.PI / 180.0);
            g2.translate(0, -dim.width);
        }        
    }

    @Override
    public Dimension getPreferredSize(JComponent c) {
        SlidingButton b = (SlidingButton) c;
        if (b.getOrientation() == SlideBarDataModel.SOUTH) {
            renderer.setText(b.getText());
            renderer.setIcon(b.getIcon());
            Dimension dim = renderer.getPreferredSize();
            // dim.width += 20;
            renderer.setText("");
            return dim;
        } else {
            renderer.setText(b.getText());
            renderer.setIcon(b.getIcon());
            Dimension dim = renderer.getPreferredSize();
            Dimension d2 = new Dimension();
            d2.width = dim.height;
            d2.height = dim.width;
            // d2.height += 20;
            renderer.setText("");
            return d2;
        }
    }

    @Override
    protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) {
        
    }

    @Override
    protected void paintText(Graphics g, AbstractButton b, Rectangle textRect, String text) {
        
    }

    @Override
    protected void paintIcon(Graphics g, AbstractButton b, Rectangle iconRect) {
        
    }    
}